#ifndef ROOMMANAGER_H
#define ROOMMANAGER_H

#include "common.h"
#include "room.h"
#include "protocol.h"
#include "simpleprotosrv.h"
#include <exception>
#include <memory>
#include <map>
#include <typeinfo>
#include <algorithm>
#include <ace/Log_Msg.h>

class RoomManager
{
public:
    ~RoomManager();
    static std::auto_ptr<RoomManager>& getInstance();

    quint32   size() const {return _room_map.size();}
    Room *    getRoomByName(const std::string& room_name);
    Room *    getRoomByClient(const Client& client);
    qint32    createRoom(ACE_SOCK_Stream &peer,const std::string &name,const std::string &password = "");
    qint32    addClient2Room(ACE_SOCK_Stream &peer,const std::string &room_name,const std::string &password = "");
    bool      removeClientFromRoom(ACE_SOCK_Stream &peer,const std::string &room_name);
    bool      removeClient(ACE_HANDLE handle);

    bool      exist(const std::string& room_name) {return getRoomByName(room_name) != 0;}

    void      dump(std::map<std::string,std::pair<quint8,bool> > &);
    bool      broadcast(const std::string &room,std::auto_ptr<SimpleProtoSrv> &msg);
protected:
    RoomManager(){}
    RoomManager(const RoomManager &);
    RoomManager& operator=(const RoomManager &);

private:
    static std::auto_ptr<RoomManager>   _mgr;
    std::map<ACE_HANDLE,std::string>	_client_map;
    std::map<std::string,Room *>        _room_map;
};

#endif // ROOMMANAGER_H
